/**
 * @file
 * Dynamic pool memory manager
 *
 * lwIP has dedicated pools for many structures (netconn, protocol control blocks,
 * packet buffers, ...). All these pools are managed here.
 */

/*
 * Copyright (c) 2001-2004 Swedish Institute of Computer Science.
 * All rights reserved.
 * Copyright (c) <2013-2015>, <Huawei Technologies Co., Ltd>
 * All rights reserved
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice,
 *    this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright notice,
 *    this list of conditions and the following disclaimer in the documentation
 *    and/or other materials provided with the distribution.
 * 3. The name of the author may not be used to endorse or promote products
 *    derived from this software without specific prior written permission.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT
 * SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT
 * OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
 * OF SUCH DAMAGE.
 *
 * This file is part of the lwIP TCP/IP stack.
 *
 * Author: Adam Dunkels <adam@sics.se>
 *
 */

/**********************************************************************************
 * Notice of Export Control Law
 * ===============================================
 * Huawei LiteOS may be subject to applicable export control laws and regulations, which
 * might include those applicable to Huawei LiteOS of U.S. and the country in which you
 * are located.
 * Import, export and usage of Huawei LiteOS in any manner by you shall be in compliance
 * with such applicable export control laws and regulations.
 **********************************************************************************/

#include "lwip/opt.h"

#include "lwip/memp.h"
#include "lwip/pbuf.h"
#include "lwip/udp.h"
#include "lwip/raw.h"
#include "lwip/tcp_impl.h"
#include "lwip/igmp.h"
#include "lwip/api.h"
#include "lwip/api_msg.h"
#include "lwip/tcpip.h"
#include "lwip/sys.h"
#include "lwip/timers.h"
#include "lwip/stats.h"
#include "netif/etharp.h"
#include "lwip/ip_frag.h"
#if LWIP_SNMP
#include "lwip/snmp_structs.h"
#include "lwip/snmp_msg.h"
#endif
#include "lwip/dns.h"
#if PPPOE_SUPPORT
#include "netif/ppp_oe.h"
#endif

#include <string.h>


#if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */

static struct memp *memp_start[MEMP_MAX];
static struct memp *memp_end[MEMP_MAX];

struct memp {
  struct memp *next;
#if MEMP_OVERFLOW_CHECK
  const char *file;
  int line;
#endif /* MEMP_OVERFLOW_CHECK */
};

#if MEMP_OVERFLOW_CHECK
/* if MEMP_OVERFLOW_CHECK is turned on, we reserve some bytes at the beginning
 * and at the end of each element, initialize them as 0xcd and check
 * them later. */
/* If MEMP_OVERFLOW_CHECK is >= 2, on every call to memp_malloc or memp_free,
 * every single element in each pool is checked!
 * This is VERY SLOW but also very helpful. */
/* MEMP_SANITY_REGION_BEFORE and MEMP_SANITY_REGION_AFTER can be overridden in
 * lwipopts.h to change the amount reserved for checking. */
#ifndef MEMP_SANITY_REGION_BEFORE
#define MEMP_SANITY_REGION_BEFORE  16
#endif /* MEMP_SANITY_REGION_BEFORE*/
#if MEMP_SANITY_REGION_BEFORE > 0
#define MEMP_SANITY_REGION_BEFORE_ALIGNED    LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_BEFORE)
#else
#define MEMP_SANITY_REGION_BEFORE_ALIGNED    0
#endif /* MEMP_SANITY_REGION_BEFORE*/
#ifndef MEMP_SANITY_REGION_AFTER
#define MEMP_SANITY_REGION_AFTER   16
#endif /* MEMP_SANITY_REGION_AFTER*/
#if MEMP_SANITY_REGION_AFTER > 0
#define MEMP_SANITY_REGION_AFTER_ALIGNED     LWIP_MEM_ALIGN_SIZE(MEMP_SANITY_REGION_AFTER)
#else
#define MEMP_SANITY_REGION_AFTER_ALIGNED     0
#endif /* MEMP_SANITY_REGION_AFTER*/

/* MEMP_SIZE: save space for struct memp and for sanity check */
#define MEMP_SIZE          (LWIP_MEM_ALIGN_SIZE(sizeof(struct memp)) + MEMP_SANITY_REGION_BEFORE_ALIGNED)
#define MEMP_ALIGN_SIZE(x)  (LWIP_MEM_ALIGN_SIZE(x) + MEMP_SANITY_REGION_AFTER_ALIGNED)
#else /* MEMP_OVERFLOW_CHECK */

/* No sanity checks
 * We don't need to preserve the struct memp while not allocated, so we
 * can save a little space and set MEMP_SIZE to 0.
 */
#define MEMP_SIZE           0
#define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))

#endif /* MEMP_OVERFLOW_CHECK */

/** This array holds the first free element of each pool.
 *  Elements form a linked list. */
static struct memp *memp_tab[MEMP_MAX];

#else /* MEMP_MEM_MALLOC */

#define MEMP_ALIGN_SIZE(x) (LWIP_MEM_ALIGN_SIZE(x))

#endif /* MEMP_MEM_MALLOC */

/** This array holds the element sizes of each pool. */
#if !MEM_USE_POOLS && !MEMP_MEM_MALLOC
static
#endif
const u16_t memp_sizes[MEMP_MAX] = {
#define LWIP_MEMPOOL(name,num,size,desc) LWIP_MEM_ALIGN_SIZE(size),
/*lint -e451 */
#include "lwip/memp_std.h"
/*lint +e451 */
};

#if !MEMP_MEM_MALLOC /* don't build if not configured for use in lwipopts.h */

/** This array holds the number of elements in each pool. */
static u16_t memp_num[MEMP_MAX] = {0};
/** This array holds a textual description of each pool. */
#ifdef LWIP_DEBUG
static const char *memp_desc[MEMP_MAX] = {
#define LWIP_MEMPOOL(name,num,size,desc)  (desc),
/*lint -e451*/
#include "lwip/memp_std.h"
/*lint +e451*/
};
#endif /* LWIP_DEBUG */

#if MEMP_SANITY_CHECK
/**
 * Check that memp-lists don't form a circle, using "Floyd's cycle-finding algorithm".
 */
LWIP_STATIC int
memp_sanity(void)
{
  s16_t i;
  struct memp *t, *h;

  for (i = 0; i < MEMP_MAX; i++) {
    t = memp_tab[i];
    if(t != NULL) {
      for (h = t->next; (t != NULL) && (h != NULL); t = t->next,
        h = (((h->next != NULL) && (h->next->next != NULL)) ? h->next->next : NULL)) {
        if (t == h) {
          return 0;
        }
      }
    }
  }
  return 1;
}
#endif /* MEMP_SANITY_CHECK*/
#if MEMP_OVERFLOW_CHECK
#if defined(LWIP_DEBUG) && MEMP_STATS
static const char * memp_overflow_names[] = {
#define LWIP_MEMPOOL(name,num,size,desc) "/"desc,
/*lint -e451*/
#include "lwip/memp_std.h"
/*lint +e451*/
  };
#endif

/**
 * Check if a memp element was victim of an overflow
 * (e.g. the restricted area after it has been altered)
 *
 * @param p the memp element to check
 * @param memp_type the pool p comes from
 */
LWIP_STATIC void
memp_overflow_check_element_overflow(struct memp *p, u16_t memp_type)
{
  u16_t k;
  u8_t *m;
#if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
  m = (u8_t*)p + MEMP_SIZE + memp_sizes[memp_type];
  for (k = 0; k < MEMP_SANITY_REGION_AFTER_ALIGNED; k++) {
    if (m[k] != 0xcd) {
      char errstr[128] = "detected memp overflow in pool ";
      char digit_val[] = "0";
      if(memp_type >= 10) {
        digit_val[0] = (char)('0' + (memp_type/10));
        if (strcat_s(errstr, (sizeof(errstr) - strlen(errstr)), digit_val) != 0) {
          return;
        }
      }
      digit_val[0] = (char)('0' + (memp_type%10));
      if (strcat_s(errstr, (sizeof(errstr) - strlen(errstr)), digit_val) != 0) {
        return;
      }
#if defined(LWIP_DEBUG) && MEMP_STATS
      if (strcat_s(errstr, (sizeof(errstr) - strlen(errstr)), memp_overflow_names[memp_type]) != 0) {
        return;
      }
#endif
      LWIP_ASSERT(errstr, 0);
    }
  }
#endif
}

/**
 * Check if a memp element was victim of an underflow
 * (e.g. the restricted area before it has been altered)
 *
 * @param p the memp element to check
 * @param memp_type the pool p comes from
 */
LWIP_STATIC void
memp_overflow_check_element_underflow(struct memp *p, u16_t memp_type)
{
  u16_t k;
  u8_t *m;
#if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
  m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
  for (k = 0; k < MEMP_SANITY_REGION_BEFORE_ALIGNED; k++) {
    if (m[k] != 0xcd) {
      char errstr[128] = "detected memp underflow in pool ";
      char digit[] = "0";
      if(memp_type >= 10) {
        digit[0] = (char)('0' + (memp_type/10));
        if (strcat_s(errstr, (sizeof(errstr) - strlen(errstr)), digit) != 0) {
          return;
        }
      }
      digit[0] = (char)('0' + (memp_type%10));
      if (strcat_s(errstr, (sizeof(errstr) - strlen(errstr)), digit) != 0) {
        return;
      }
#if defined(LWIP_DEBUG) && MEMP_STATS
      if (strcat_s(errstr, (sizeof(errstr) - strlen(errstr)), memp_overflow_names[memp_type]) != 0) {
        return;
      }
#endif
      LWIP_ASSERT(errstr, 0);
    }
  }
#endif
}

/**
 * Do an overflow check for all elements in every pool.
 *
 * @see memp_overflow_check_element for a description of the check
 */
LWIP_STATIC void
memp_overflow_check_all(void)
{
  u16_t i, j;
  struct memp *p;

  p = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
  for (i = 0; i < MEMP_MAX; ++i) {
    for (j = 0; j < memp_num[i]; ++j) {
      memp_overflow_check_element_overflow(p, i);
      p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
    }
  }
  p = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
  for (i = 0; i < MEMP_MAX; ++i) {
    for (j = 0; j < memp_num[i]; ++j) {
      memp_overflow_check_element_underflow(p, i);
      p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
    }
  }
}

/**
 * Initialize the restricted areas of all memp elements in every pool.
 */
LWIP_STATIC void
memp_overflow_init(void)
{
  u16_t i, j;
  struct memp *p;
  u8_t *m;

  p = (struct memp *)LWIP_MEM_ALIGN(memp_memory);
  for (i = 0; i < MEMP_MAX; ++i) {
    for (j = 0; j < memp_num[i]; ++j) {
#if MEMP_SANITY_REGION_BEFORE_ALIGNED > 0
      m = (u8_t*)p + MEMP_SIZE - MEMP_SANITY_REGION_BEFORE_ALIGNED;
      (void)memset_s(m, MEMP_SANITY_REGION_BEFORE_ALIGNED, 0xcd, MEMP_SANITY_REGION_BEFORE_ALIGNED);
#endif
#if MEMP_SANITY_REGION_AFTER_ALIGNED > 0
      m = (u8_t*)p + MEMP_SIZE + memp_sizes[i];
      (void)memset_s(m, MEMP_SANITY_REGION_AFTER_ALIGNED, 0xcd, MEMP_SANITY_REGION_AFTER_ALIGNED);
#endif
      p = (struct memp*)((u8_t*)p + MEMP_SIZE + memp_sizes[i] + MEMP_SANITY_REGION_AFTER_ALIGNED);
    }
  }
}
#endif /* MEMP_OVERFLOW_CHECK */

unsigned int g_lwip_num_sockets = DEFAULT_LWIP_NUM_SOCKETS;
int lwip_set_socket_num(int socketnum)
{
    extern int tcpip_init_finish;
    SYS_ARCH_DECL_PROTECT(old_level);
    if (tcpip_init_finish) {
        PRINTK("%s failed! call this api before tcpip_init\n", __FUNCTION__);
        return -1;
    }
    if (socketnum <= 0 || socketnum > MAX_LWIP_SOCKET_NUM){
        PRINTK("set failed! legal socketnum range[%d, %d]\n", 1, MAX_LWIP_SOCKET_NUM);
        return -1;
    }
    SYS_ARCH_PROTECT(old_level);
    g_lwip_num_sockets = (unsigned int) socketnum;
    SYS_ARCH_UNPROTECT(old_level);

    return 0;

}

int lwip_get_socket_num(void)
{
    int ret;
    SYS_ARCH_DECL_PROTECT(old_level);
    SYS_ARCH_PROTECT(old_level);
    ret = (int)g_lwip_num_sockets;
    SYS_ARCH_UNPROTECT(old_level);
    return ret;
}

/**
 * Initialize this module.
 *
 * Carves out memp_memory into linked lists for each pool-type.
 */
void
memp_init(void)
{
  struct memp *memp;
  u16_t i = 0, j;

  u32_t memp_size = 0;
  u8_t *memp_start_addr = NULL;

  memp_size = 0
  #define LWIP_MEMPOOL(name,num,size,desc) + ( (num) * (MEMP_SIZE + MEMP_ALIGN_SIZE(size) ) )
  /*lint -e451*/
  #include "lwip/memp_std.h"
  /*lint +e451*/
  ;

  /* this space will not be free, use memp_start_addr as static pool */
  memp_start_addr = mem_malloc(memp_size);
  LWIP_ERROR("memp_init: memp_start_addr=NULL", (memp_start_addr != NULL), return;);
  (void)memset_s(memp_start_addr, memp_size, 0, memp_size);

  #define LWIP_MEMPOOL(name,num,size,desc) memp_num[i++] = (num);
  /*lint -e451*/
  #include "lwip/memp_std.h"
  /*lint +e451*/

  for (i = 0; i < MEMP_MAX; ++i) {
    MEMP_STATS_AVAIL(used, i, 0);
    MEMP_STATS_AVAIL(max, i, 0);
    MEMP_STATS_AVAIL(err, i, 0);
    MEMP_STATS_AVAIL(avail, i, memp_num[i]);
  }

  memp = (struct memp *)memp_start_addr;
  /* for every pool: */
  for (i = 0; i < MEMP_MAX; ++i) {
    memp_tab[i] = NULL;
    memp_start[i] = memp;
    /* create a linked list of memp elements */
    for (j = 0; j < memp_num[i]; ++j) {
      memp->next = memp_tab[i];
      memp_tab[i] = memp;
      if (j == memp_num[i]-1) {
        memp_end[i] = memp;
      }
      memp = (struct memp *)(void *)((u8_t *)memp + MEMP_SIZE + memp_sizes[i]
#if MEMP_OVERFLOW_CHECK
        + MEMP_SANITY_REGION_AFTER_ALIGNED
#endif
      );
    }
  }
#if MEMP_OVERFLOW_CHECK
  memp_overflow_init();
  /* check everything a first time to see if it worked */
  memp_overflow_check_all();
#endif /* MEMP_OVERFLOW_CHECK */
}

/**
 * Get an element from a specific pool.
 *
 * @param type the pool to get an element from
 *
 * the dbg version has two more parameters:
 * @param file file name calling this function
 * @param line number of line where this function is called
 *
 * @return a pointer to the allocated memory or a NULL pointer on error
 */
void *
#if !MEMP_OVERFLOW_CHECK
memp_malloc(memp_t type)
#else
memp_malloc_fn(memp_t type, const char* file, const int line)
#endif
{
  struct memp *memp;
  SYS_ARCH_DECL_PROTECT(old_level);
  LWIP_ERROR("memp_malloc: type < MEMP_MAX", (type < MEMP_MAX), return NULL;);

  SYS_ARCH_PROTECT(old_level);
#if MEMP_OVERFLOW_CHECK >= 2
  memp_overflow_check_all();
#endif /* MEMP_OVERFLOW_CHECK >= 2 */
  memp = memp_tab[type];
  if (memp != NULL) {
    memp_tab[type] = memp->next;
#if MEMP_OVERFLOW_CHECK
    memp->next = NULL;
    memp->file = file;
    memp->line = line;
#endif /* MEMP_OVERFLOW_CHECK */
    MEMP_STATS_INC_USED(used, type);
    LWIP_ASSERT("memp_malloc: memp properly aligned",
                ((mem_ptr_t)memp % MEM_ALIGNMENT) == 0);
    memp = (struct memp*)(void *)((u8_t*)memp + MEMP_SIZE);
  } else {
    LWIP_DEBUGF(MEMP_DEBUG | LWIP_DBG_LEVEL_SERIOUS, ("memp_malloc: out of memory in pool %s\n", memp_desc[type]));
    MEMP_STATS_INC(err, type);
  }

  SYS_ARCH_UNPROTECT(old_level);

  return memp;
}

/**
 * Put an element back into its pool.
 *
 * @param type the pool where to put mem
 * @param mem the memp element to free
 */
void
memp_free(memp_t type, void *mem)
{
  struct memp *memp;
  SYS_ARCH_DECL_PROTECT(old_level);

  if (mem == NULL) {
    return;
  }
  LWIP_ASSERT("memp_free: mem properly aligned",
                ((mem_ptr_t)mem % MEM_ALIGNMENT) == 0);

  memp = (struct memp *)(void *)((u8_t*)mem - MEMP_SIZE);

  SYS_ARCH_PROTECT(old_level);
#if MEMP_OVERFLOW_CHECK
#if MEMP_OVERFLOW_CHECK >= 2
  memp_overflow_check_all();
#else
  memp_overflow_check_element_overflow(memp, type);
  memp_overflow_check_element_underflow(memp, type);
#endif /* MEMP_OVERFLOW_CHECK >= 2 */
#endif /* MEMP_OVERFLOW_CHECK */

  MEMP_STATS_DEC(used, type);

  memp->next = memp_tab[type];
  memp_tab[type] = memp;

#if MEMP_SANITY_CHECK
  LWIP_ASSERT("memp sanity", memp_sanity());
#endif /* MEMP_SANITY_CHECK */

  SYS_ARCH_UNPROTECT(old_level);
}

u32_t
memp_check(memp_t type, void *mem)
{
  if (mem >= (void*)memp_start[type] && mem <= (void*)memp_end[type]) {
    return 1;
  } else {
    return 0;
  }
}

#ifdef LWIP_DEBUG_INFO
void debug_memp_info(void)
{
  int type;
  u32_t memp_used;
  u32_t memp_aval;
  struct memp *memp = NULL;

  LWIP_PLATFORM_PRINT("[MEMP]%-4s %-10s %-10s %-10s %-8s %-8s %-8s\n",
    "TYPE", "START", "END", "TAB", "NUM", "USED", "AVALIABLE");
  for (type = 0; type < MEMP_MAX; type++) {
    memp_aval = 0;
    memp = memp_tab[type];
    while (memp != NULL) {
      memp_aval++;
      memp = memp->next;
    }
    memp_used = memp_num[type] - memp_aval;

    LWIP_PLATFORM_PRINT("[MEMP]%-4d ", type);
    LWIP_PLATFORM_PRINT("%-10p ", memp_start[type]);
    LWIP_PLATFORM_PRINT("%-10p ", memp_end[type]);
    LWIP_PLATFORM_PRINT("%-10p ", memp_tab[type]);
    LWIP_PLATFORM_PRINT("%-8u  ", memp_num[type]);
    LWIP_PLATFORM_PRINT("%-8u  ", memp_used);
    LWIP_PLATFORM_PRINT("%-8u\n", memp_aval);
  }
}


void debug_memp_detail(int type)
{
  u32_t index;
  struct memp *memp = NULL;

  if (type >= MEMP_MAX || type < 0) {
    LWIP_PLATFORM_PRINT("[MEMP]memp type should < %u and >= 0\n", type);
    return;
  }

  LWIP_PLATFORM_PRINT("[MEMP]%-4s %-8s %-10s %-10s\n",
    "TYPE", "INDEX", "ADDR", "NEXT");
  for (index = 0; index < memp_num[type]; index++) {
    memp = (struct memp *)(void *)((u8_t *)memp_start[type] + index*(MEMP_SIZE + memp_sizes[type]
#if MEMP_OVERFLOW_CHECK
      + MEMP_SANITY_REGION_AFTER_ALIGNED
#endif
      ));
    LWIP_PLATFORM_PRINT("[MEMP]%-4d ", type);
    LWIP_PLATFORM_PRINT("%-8u ",   index);
    LWIP_PLATFORM_PRINT("%-10p ",  memp);
    LWIP_PLATFORM_PRINT("%-10p\n", memp->next);
  }
}


int debug_memp_used_check(int type, struct memp *ptr)
{
  struct memp *memp = NULL;

  memp = memp_tab[type];
  while (memp != NULL) {
    if (memp == ptr) {
      return 0;
    }
    memp = memp->next;
  }

  return 1;
}


void debug_memp_type_info(int type, int detail)
{
  struct memp *memp = NULL;

  if (type >= MEMP_MAX || type < 0) {
    LWIP_PLATFORM_PRINT("[MEMP]memp type should < %u and >= 0\n", type);
    return;
  }

  memp = memp_end[type];
  while (memp != memp_start[type]) {
    if (debug_memp_used_check(type, memp)) {
      switch (type) {
        case MEMP_TCP_PCB:
          (void)debug_tcppcb_info((void *)((u8_t*)memp + MEMP_SIZE), detail);
          break;
        case MEMP_UDP_PCB:
          (void)debug_udppcb_info((void *)((u8_t*)memp + MEMP_SIZE), detail);
          break;
        case MEMP_RAW_PCB:
          (void)debug_rawpcb_info((void *)((u8_t*)memp + MEMP_SIZE), detail);
          break;
        case MEMP_NETCONN:
          (void)debug_netconn_info((void *)((u8_t*)memp + MEMP_SIZE), 0, detail);
          break;
        default:
          LWIP_PLATFORM_PRINT("memp type error!\n");
          return;
      }
    }
    memp = (struct memp *)(void *)((u8_t *)memp - (MEMP_SIZE + memp_sizes[type]
#if MEMP_OVERFLOW_CHECK
      + MEMP_SANITY_REGION_AFTER_ALIGNED
#endif
      ));
  }
}

#if LWIP_DEBUG_TCP_LEAK
void debug_memp_leak_info(int type)
{
  struct memp *memp = NULL;

  if (type >= MEMP_MAX || type < 0) {
    LWIP_PLATFORM_PRINT("[MEMP]memp type should < %u and >= 0\n", type);
    return;
  }

  memp = memp_end[type];
  while (memp != memp_start[type]) {
    if (debug_memp_used_check(type, memp)) {
      switch (type) {
        case MEMP_TCP_PCB:
          (void)debug_tcppcb_leak((void *)((u8_t*)memp + MEMP_SIZE));
          break;
        case MEMP_NETCONN:
          (void)debug_netconn_leak((void *)((u8_t*)memp + MEMP_SIZE));
          break;
        default:
          LWIP_PLATFORM_PRINT("memp type error!\n");
          return;
      }
    }
    memp = (struct memp *)(void *)((u8_t *)memp - (MEMP_SIZE + memp_sizes[type]
#if MEMP_OVERFLOW_CHECK
      + MEMP_SANITY_REGION_AFTER_ALIGNED
#endif
      ));
  }
}
#endif /* LWIP_DEBUG_TCP_LEAK */

#endif /* LWIP_DEBUG_INFO */

#endif /* MEMP_MEM_MALLOC */
